<?php

/**
 * Controller_researchs
 */

class Controller_resources extends MyApp {
	/** 首页 **/
	public function actionIndex()
	{

		// 分页
		YOU::loadLib('page', "", '0');

		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_file`");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);

		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";

		$grade_lists  = $this->db->fetchAll("SELECT * FROM `tbl_grade` ORDER BY grade_id ASC ");
		if (empty($_GET['grade_id'])) {

			$class_lists  = $this->db->fetchAll("SELECT * FROM `tbl_class` WHERE `grade_id`=2 ORDER BY class_id ASC ");
		}else{
			$grade_id    = $this->get->grade_id;
			$class_lists = $this->db->fetchAll("SELECT * FROM `tbl_class` WHERE `grade_id`={$grade_id} ORDER BY class_id ASC ");
		}
		$dir_lists=$this->db->fetchAll("SELECT * FROM `tbl_dir` ORDER BY dir_type ASC ");
		$dir_id="f.`dir_id`=d.`dir_id`";
		$file_lists=$this->db->fetchAll("SELECT * FROM `tbl_file_folder` ");
		$name_lists=$this->db->fetchAll("SELECT * FROM `tbl_file` AS f inner join `tbl_dir` as d WHERE {$dir_id} ORDER BY f.`file_id` DESC LIMIT {$start}, ".PAGE_LIMIT."");
		$user_lists=$this->db->fetchAll("SELECT * FROM `tbl_class` ORDER by class_id ASC" );
		$this->tpl->assign("user_lists",$user_lists);
		$this->tpl->assign("name_lists",$name_lists);
		$this->tpl->assign("file_lists",$file_lists);
		$this->tpl->assign("dir_lists",$dir_lists);
		$this->tpl->assign("class_lists",$class_lists);
		$this->tpl->assign("grade_lists",$grade_lists);
		$this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->display("resources/resources.index.tpl.php");
	}
	public function actionAdd()
	{
		$dir_lists=$this->db->fetchAll("SELECT * FROM `tbl_dir`");

		if ($_POST) {

			$file_theme = $this->post->file_theme;
			$file_sec   = $this->post->file_sec;
			$dir_id     = $this->post->dir_id;
			if (!empty($_FILES)) {
				$file_name=$_FILES['file_name']["name"];
				$newName=md5(uniqid(date("YmdHis"),true));
				$ext=pathinfo($file_name);
				$newName=$newName.".".$ext['extension'];
				$toPath=UPLOAD_PATH."/teacher_in";
				$dest=$toPath.'/'.$newName;
				$tmp_name=$_FILES['file_name']['tmp_name'];
				if (copy($tmp_name, $dest)) {
					$name=$dest;

				}
				$info=array(
				"file_theme"=>$file_theme,
				"file_sec"=>$file_sec,
				"dir_id"=>$dir_id,
				"file_name"=>$name,
				);

			}
			$this->db->do_insert("tbl_file", $info);
			// 跳转
			Helper_Client::location("添加成功", "index.php?a=resources.index&file_id= {$file_id}");

		}
		$this->tpl->assign("dir_lists",$dir_lists);
		$this->tpl->display("resources/resources.add.tpl.php");
	}
	public function actionInfo()
	{
		if ($_POST) {
			$file_id=$this->post->file_id;
			$file_msg=$this->post->file_msg;
			if (!empty($file_msg)) {
				$info=array(
				"file_msg" =>$file_msg,
				);
				$this->db->do_update("tbl_file", $info, "file_id='{$file_id}'");
				// 跳转
				Helper_Client::location("留言成功", "index.php?a=resources.index&file_id= {$file_id}");
			}
		}

		if ($_GET) {
			$file_id   = $this->get->file_id;
			$file_data = $this->db->fetchRow("SELECT * FROM `tbl_file` WHERE `file_id`={$file_id} ");

		}
		$this->tpl->assign("file_data",$file_data);
		$this->tpl->display("resources/resources.info.tpl.php");
	}
	public function actionCollect(){
		$this->tpl->display("resources/resources.col.tpl.php");
	}

	/**  **/
	public function actionList(){

		$file_type=$this->get->file_type;
		// 分页
		YOU::loadLib('page', "", '0');

		$tmp   = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_file`");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);

		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";
		$dir_id="f.`dir_id`=d.`dir_id`";
		$name_lists=$this->db->fetchAll("SELECT * FROM `tbl_file` AS f inner join `tbl_dir` as d WHERE {$dir_id} ORDER BY f.`file_id` DESC LIMIT {$start}, ".PAGE_LIMIT."");
		$this->tpl->assign("name_lists",$name_lists);
		$this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->display("resources/resources.list.tpl.php");


	}
	public function actionType(){
		$dir_id=$this->get->dir_id;
		// 分页
		YOU::loadLib('page', "", '0');

		$tmp   = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_file`");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);

		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";

		$name_lists=$this->db->fetchAll("SELECT * FROM `tbl_file`  WHERE {$user_id}  `dir_id`='{$dir_id}' ORDER BY  `file_id` DESC LIMIT {$start}, ".PAGE_LIMIT."");
		$this->tpl->assign("name_lists",$name_lists);
		$this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->display("resources/resources.list.tpl.php");
	}
	public function actionExt(){
		//收藏
		$file_id=$this->get->file_id;
		$file_data=$this->db->fetchRow("SELECT * FROM `tbl_file` WHERE file_id='{$file_id}' ");
		$file_down=$file_data['file_down']+1;
		$info=array(
		"file_down" => $file_down,
		);
		$this->db->do_update("tbl_file", $info, "file_id='{$file_id}'");
		// 跳转
		Helper_Client::location("收藏成功", "index.php?a=resources.index&file_id= {$file_id}");

	}
	public function actionDel(){
		$file_id=$this->get->file_id;
		$this->db->query("DELETE  FROM `tbl_file` WHERE `file_id`='{$file_id}'");

		// 跳转
		Helper_Client::location("删除文件成功", "index.php?a=resources.index&file_id= {$file_id}");
	}
	public function actionUpdate(){
		$file_id=$this->get->file_id;
		$file_data=$this->db->fetchRow("SELECT * FROM `tbl_file` WHERE `file_id`={$file_id} ");
		if ($_POST) {
			$file_id    = $this->post->file_id;
			$file_theme = $this->post->file_theme;
			$file_sec   = $this->post->file_sec;
			$file_date  = $this->post->file_date;
			$user_id    = $this->post->user_id;
			if (!empty($_FILES)) {
				$file_name=$_FILES['file_name']["name"];
				$newName=md5(uniqid(date("YmdHis"),true));
				$ext=pathinfo($file_name);
				$newName=$newName.".".$ext['extension'];
				$toPath=UPLOAD_PATH."/teacher_in";
				$dest=$toPath.'/'.$newName;
				$tmp_name=$_FILES['file_name']['tmp_name'];
				if (copy($tmp_name, $dest)) {
					$name=$dest;

				}
				$info=array(
				"file_theme" => $file_theme,
				"file_sec"   => $file_sec,
				"file_date"  => $file_date,
				"user_id"    => $user_id,
				"file_name"  => $name,
				);

			}

			$this->db->do_update("tbl_file", $info, "file_id='{$file_id}'");
			// 跳转
			Helper_Client::location("修改成功", "index.php?a=resources.index&file_id= {$file_id}");

		}
		$this->tpl->assign("file_data",$file_data);
		$this->tpl->display("resources/resources.update.tpl.php");
	}
	public function actionDown(){
		$file_id=$this->get->file_id;
		$file_data=$this->db->fetchRow("SELECT * FROM `tbl_file` WHERE `file_id`={$file_id} ");
		$path=$file_data['file_name'];
		$path=str_replace("/", "\\", $path);
		$file_name=explode("\\", $path);
		$file_name=array_pop($file_name);


		//首先要判断给定的文件存在与否
		if(!file_exists($path)){
			echo "没有该文件文件";
			return ;
		}
		$fp=fopen($path,"r");
		$file_size=filesize($path);
		//下载文件需要用到的头
		Header("Content-type: application/octet-stream");
		Header("Accept-Ranges: bytes");
		Header("Accept-Length:".$file_size);
		Header("Content-Disposition: attachment; filename=".$file_name);
		$buffer=1024;
		$file_count=0;
		//向浏览器返回数据
		while(!feof($fp) && $file_count<$file_size){
			$file_con=fread($fp,$buffer);
			$file_count+=$buffer;
			echo $file_con;
		}

		fclose($fp);


	}
	public function actionSearch(){
		if ($_POST) {
			$class_id  = $this->post->class_id;
			$file_date = $this->post->file_date;

			// 分页
			YOU::loadLib('page', "", '0');

			$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_file`");
			$count = $tmp['count'];
			$page_total = ceil($count/PAGE_LIMIT);

			// 当前页
			$p = intval($_GET['p']);
			if ($p == 0){
				$p  = 1;
			}
			if ($p > 1){
				$start = PAGE_LIMIT * ($p-1);
			}else{
				$start = 0;
			}

			$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
			$page->next_page   = '下一页>';
			$page->pre_page    = '<上一页';
			$page->style_path  = "static/page_style";
			$dir_id="f.`dir_id`=d.`dir_id`";
			$name_lists=$this->db->fetchAll("SELECT * FROM `tbl_file` WHERE `class_id`='{$class_id}' LIMIT {$start}, ".PAGE_LIMIT."");

		}
		$this->tpl->assign("name_lists",$name_lists);

		$this->tpl->display("resources/resources.list.tpl.php");

	}

}
?>